package edu.gmu.foxhound;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.widget.Toast;

//this class will read the latest GPS and sends it externally 


public class LocationSendBroadcastReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {

		//get the preferences
		SharedPreferences settings = context.getSharedPreferences(context.getString(R.string.settings), Context.MODE_PRIVATE);

		//if settings can be found
		if(settings!=null){

			System.out.println("Location BroadcastReceiver settings found");
			//alarm is turned off or malformed settings

			//System.out.println("settings alarm interval"+ settings.getLong(context.getString(R.string.settings_alarm_interval), -1L));
			if(settings.getLong(context.getString(R.string.settings_alarm_interval), 0L) == 0L){
				Toast.makeText(context, "Foxhound disabled", Toast.LENGTH_SHORT).show();
			}
			else{

				//System.out.println("Location BroadcastReceiver getting location");
				//get location
				LocationManager locMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
				Location loc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

				//if location was not null, send the last known location
				if(loc != null){
					//TODO:For now, just send toast message

					//System.out.println("Location BroadcastReceiver checking for silent toast");
					if(settings.getBoolean(context.getString(R.string.settings_toast_mode_silent), false)){
						Toast.makeText(context, loc.getLatitude()+","+loc.getLongitude(), Toast.LENGTH_SHORT).show();
					}

/*
					//send 
					HttpClient client = new DefaultHttpClient();

					HttpPost post = new HttpPost(loc.getLatitude()+","+loc.getLongitude());
					try {
						@SuppressWarnings("unused")
						HttpResponse response = client.execute(post);


					} catch (ClientProtocolException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					*/
				}
				else{
					System.out.println("location null");
				}

				//reschedule the next intent
				//System.out.println("Location BroadcastReceiver creating next alarm");
				Util.CreateAlarm(
						context, 
						settings.getLong(context.getString(R.string.settings_alarm_interval), 
								Long.parseLong(context.getString(R.string.settings_alarm_interval_val)))
						);

			}
		}
	}

}
